using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using VisualWip.Core;
using VisualWip.Core.Columns;
using VisualWip.Core.Interfaces;
using VisualWip.Core.Utils;
using VisualWip.PresentationModel.SettingsViewModels;
using VisualWip.TFS2010Common;

namespace VisualWip.TFS2010Providers
{
    public abstract class TfsWorkQuerySettingsViewModelBase<TColumn> : WorkItemsSettingsViewModelBase<TColumn>
        where TColumn : WorkColumnBase
    {
        protected TfsConnection Connection;
        private bool _downloadDisplayFieldsOnly;


        protected TfsWorkQuerySettingsViewModelBase(Application application, TColumn column,
                                                    IWorkItemSizeProvider workItemSizeProvider,
                                                    IColumnTypeProvider columnTypeProvider)
            : base(application, column, workItemSizeProvider, columnTypeProvider)
        {
            Init();
            PropertyChanged +=new PropertyChangedEventHandler(MyPropertyChanged);
        }

        private void MyPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertyHelper.Name(() => DownloadDisplayFieldsOnly))
            {
                DownloadDisplayFieldsOnlyHasChanged();
            }
        }

        protected abstract void DownloadDisplayFieldsOnlyHasChanged();


        public int TotalWorkInProgressLimit
        {
            get { return Column.TotalWorkInProgressLimit; }
            set
            {
                if (value != Column.TotalWorkInProgressLimit)
                {
                    Column.TotalWorkInProgressLimit = value;
                    OnPropertyChanged(this, () => TotalWorkInProgressLimit);
                }
            }
        }

        public bool DownloadDisplayFieldsOnly
        {
            get { return _downloadDisplayFieldsOnly; }
            set
            {
                if (_downloadDisplayFieldsOnly != value)
                {
                    _downloadDisplayFieldsOnly = value;
                    OnPropertyChanged(this, () => DownloadDisplayFieldsOnly);
                }
            }
        }

        protected virtual void Init()
        {
            var query = Column.Query as WorkItemQueryBase;
            if (query != null)
                Connection = query.Connection;
            else
            {
                Connection = new TfsConnection();
                Connection.Connect();
            }

        }

        protected void Attache(ObservableCollection<QueryItemViewModel> queryItemViewModels,
                               WorkItemQueryBase selectedQuery,
                               PropertyChangedEventHandler queryItemPropertyChanged)
        {
            foreach (QueryItemViewModel queryItemViewModel in queryItemViewModels)
            {
                if (queryItemViewModel is QueryFolderViewModel)
                    Attache(queryItemViewModel.Children, selectedQuery, queryItemPropertyChanged);
                else
                {
                    queryItemViewModel.PropertyChanged += queryItemPropertyChanged;
                    if (selectedQuery != null && queryItemViewModel.Id == selectedQuery.QueryId)
                        queryItemViewModel.IsSelected = true;
                }
            }
        }

        protected WorkItemQueryBase CreateWorkItemQuery(QueryDefinitionViewModel queryDefinitionViewModel)
        {
            if (queryDefinitionViewModel.QueryType == "List")
                return new WorkItemListQuery(Connection)
                           {
                               QueryId = queryDefinitionViewModel.Id,
                               DownloadDisplayFieldsOnly = DownloadDisplayFieldsOnly,
                           };

            return new WorkItemHierarchicalQuery(Connection)
                       {
                           QueryId = queryDefinitionViewModel.Id,
                           DownloadDisplayFieldsOnly = DownloadDisplayFieldsOnly,
                       };
        }

        protected void SetColumnName(QueryDefinitionViewModel queryDefinitionViewModel)
        {
            if (String.IsNullOrWhiteSpace(ColumnName))
                ColumnName = queryDefinitionViewModel.Name;
        }
    }
}